﻿using AvalonDock.Themes;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Sany.AGV.RobotTool.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private readonly IDialogService _dialogService;
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _aggregator;

        public MainWindowViewModel(IRegionManager regionManager, IDialogService dialog, IServiceProvider serviceProvider, IEventAggregator aggregator)
        {
            _dialogService = dialog;
            _aggregator = aggregator;
            _regionManager = regionManager;

            SelectedTheme = new Vs2013DarkTheme();
            SwitchThemeCommand = new DelegateCommand<object>(SwitchTheme);
        }

        private void SwitchTheme(object obj)
        {
            var themeName = obj.ToString();
            switch (themeName)
            {
                default:
                case "Vs2013DarkTheme":
                    {
                        SelectedTheme = new Vs2013DarkTheme();
                        Application.Current.Resources.MergedDictionaries[0].Source = new Uri("pack://application:,,,/MLib;component/Themes/DarkTheme.xaml");
                        Application.Current.Resources.MergedDictionaries[1].Source = new Uri("pack://application:,,,/Sany.AGV.RobotTool;component/Themes/DarkBrushsExtended.xaml");
                    }
                    break;
                case "Vs2013LightTheme":
                    {
                        SelectedTheme = new Vs2013LightTheme();
                        Application.Current.Resources.MergedDictionaries[0].Source = new Uri("pack://application:,,,/MLib;component/Themes/LightTheme.xaml");
                        Application.Current.Resources.MergedDictionaries[1].Source = new Uri("pack://application:,,,/Sany.AGV.RobotTool;component/Themes/LightBrushsExtended.xaml");
                    }
                    break;
                case "Vs2013BlueTheme":
                    {
                        SelectedTheme = new Vs2013BlueTheme();
                        Application.Current.Resources.MergedDictionaries[0].Source = new Uri("pack://application:,,,/MLib;component/Themes/LightTheme.xaml");
                        Application.Current.Resources.MergedDictionaries[1].Source = new Uri("pack://application:,,,/Sany.AGV.RobotTool;component/Themes/BlueBrushsExtended.xaml");
                    }
                    break;
            }
        }

        public DelegateCommand NewCommand { set; get; }
        public DelegateCommand OpenCommand { set; get; }
        public DelegateCommand SaveCommand { set; get; }
        public DelegateCommand SaveAsCommand { set; get; }
        public DelegateCommand CloseCommand { set; get; }
        public DelegateCommand<object> SwitchThemeCommand { set; get; }


        /// <summary>
        /// 
        /// </summary>
        private Theme _selectedTheme;
        public Theme SelectedTheme
        {
            get { return _selectedTheme; }
            set
            {
                _selectedTheme = value;
                RaisePropertyChanged(nameof(SelectedTheme));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private DocumentViewModel[] _documents;
        public IEnumerable<DocumentViewModel> Documents
        {
            get
            {
                if (_documents == null)
                    _documents = new DocumentViewModel[] { TextDocument };
                return _documents;
            }
        }

        private TextDocumentViewModel _textDocumentViewModel;
        public TextDocumentViewModel TextDocument
        {
            get
            {
                if (_textDocumentViewModel == null)
                {
                    _textDocumentViewModel = new TextDocumentViewModel();
                }
                return _textDocumentViewModel;
            }
        }


        private AnchorableViewModel[] _anchorables;
        public IEnumerable<AnchorableViewModel> Anchorables
        {
            get
            {
                if (_anchorables == null)
                    _anchorables = new AnchorableViewModel[] { Debug, Notification, Output, Property, Solution, Toolbox };
                return _anchorables;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private DebugViewModel _debugViewModel;
        public DebugViewModel Debug
        {
            get
            {
                if (_debugViewModel == null)
                {
                    _debugViewModel = new DebugViewModel();
                }
                return _debugViewModel;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private NotificationViewModel notification;
        public NotificationViewModel Notification
        {
            get
            {
                if (notification == null)
                {
                    notification = new NotificationViewModel();
                }
                return notification;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private OutputViewModel output;
        public OutputViewModel Output
        {
            get
            {
                if (output == null)
                {
                    output = new OutputViewModel();
                }
                return output;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private PropertyViewModel property;
        public PropertyViewModel Property
        {
            get
            {
                if (property == null)
                {
                    property = new PropertyViewModel();
                }
                return property;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private SolutionViewModel solution;
        public SolutionViewModel Solution
        {
            get
            {
                if (solution == null)
                {
                    solution = new SolutionViewModel();
                }
                return solution;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private ToolboxViewModel toolbox;
        public ToolboxViewModel Toolbox
        {
            get
            {
                if (toolbox == null)
                {
                    toolbox = new ToolboxViewModel();
                }
                return toolbox;
            }
        }
    }
}
